﻿@using bpms.Enums
@model IEnumerable<Customer>
@{
    ViewData["Title"] = "顾客管理";
}
@{
    var userTypeClaim = User.FindFirst("UserType")?.Value;
    var userType = !string.IsNullOrEmpty(userTypeClaim)
        ? (UserType)int.Parse(userTypeClaim)
        : UserType.Admin;
}
<div class="container mt-4">
    <h2 class="mb-4">@ViewData["Title"]</h2>

    <!-- 搜索表单 -->
    <form asp-action="Index" method="get" class="mb-4">
        <div class="row g-2">
            <div class="col-6 col-md-4 col-lg-3">
                <div class="input-group">
                    <input type="text"
                           name="searchKey"
                           class="form-control form-control-sm"
                           placeholder="姓名/联系电话"
                           value="@ViewBag.SearchKey">
                    <button type="submit" class="btn btn-primary btn-sm">
                        <i class="bi bi-search"></i>搜索
                    </button>
                </div>
            </div>
            @if (userType == (int)UserType.Admin)
            {
                <div class="col-auto">
                    <a asp-action="Add" class="btn btn-success btn-sm">
                        <i class="bi bi-plus-circle"></i> 新增
                    </a>
                </div>
            }
        </div>
    </form>

    <!-- 数据表格 -->
    <div class="table-responsive">
        <table class="table table-hover table-striped">
            <thead class="table-light">
                <tr>
                    <th>@Html.DisplayNameFor(m => m.LoginName)</th>
                    <th>@Html.DisplayNameFor(m => m.Name)</th>
                    <th>@Html.DisplayNameFor(m => m.Phone)</th>
                    <th>@Html.DisplayNameFor(m => m.Address)</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model)
                {
                    <tr>
                        <td>@item.LoginName</td>
                        <td>@item.Name</td>
                        <td>@item.Phone</td>
                        <td>
                            @if (!string.IsNullOrEmpty(item.Address))
                            {
                                <span class="text-truncate" style="max-width: 150px;"
                                      title="@item.Address">
                                    @item.Address
                                </span>
                            }
                        </td>
                        <td>
                            @if (userType == (int)UserType.Admin)
                            {
                                <a asp-action="Edit" asp-route-id="@item.Id" class="btn btn-sm btn-primary">编辑</a>
                            }
                            <a asp-action="Delete" asp-route-id="@item.Id"
                               class="btn btn-sm btn-danger"
                               onclick="return confirm('确定删除？')">
                                <i class="bi bi-trash"></i> 删除
                            </a>
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
</div>

@section Scripts {
    <script>
        // 地址栏过长时显示提示
        $(function(){
            $('[title]').tooltip({
                placement: 'top'
            });
        });
    </script>
}